﻿using Game.Command;
using Game.Command.Internal;
using Game.Map;
using Game.Map.Extensions;

namespace Game.Controller.Internal
{
    class AttackStratagy
    {
        private readonly IGameMap _GameMap;
        
        public AttackStratagy(IGameMap map)
        {
            _GameMap = map;
        }
        
        public void AssignCommand(IMapField field)
        {
            if (field.VehicleX().Vehicle == null)
            {
                return;
            }
            
            if (AttackValue.GetValue(field.VehicleX().Vehicle) == 0)
            {
                return;
            }

            var collection = MapUtil.GetAdjacentFields(_GameMap, field.Point);
            foreach (var cursor in collection)
            {
                var check = _GameMap.Fields[cursor.Point];
                
                bool filter = CheckFilter(check, field);
                if (filter)
                {
                    continue;
                }
                
                var attackPath = new GamePath(field.Point, cursor.Point);
                var cmd = new AttackUnitCommand(_GameMap, attackPath);
                cursor.CommandX().Command = cmd;
                cmd.Attacked += (source, args) => AttackValue.SetValue(args.Attacking, 0);
                cursor.ViewX().GameImage = GameImage.AttackArrow;
            }
        }

        bool CheckFilter(IMapField check, IMapField field)
        {
            bool filter = false;
            
            if (check.VehicleX().Vehicle == null) 
            {
                filter = true;
            }
            else
            {
                if (field.VehicleX().Vehicle.Owner.Name == check.VehicleX().Vehicle.Owner.Name)
                {
                    filter = true;
                }
            }
                
            return filter;
        }

    }
}
